<template>
	<view class="common-car">
		<view class="empty-shop-car" v-if="list.length===0">
			<image :src="`${CDN}/empty_shop_car.png`" class="empty-shop-car-image" mode=""></image>
			<text>当前您的购物车是空的</text>
			<view class="empty-shop-car-btn" @click="$tool.pageTo('/pages/goods/list')">
				去逛逛
			</view>
		</view>
		<view class="shop-car" v-else>
			<view class="shop-car-header">
				<text @click="isCut=!isCut">{{isCut?'编辑':'完成'}}</text>
			</view>
			<view class="store-box">
				<view class="goodsInfo" v-for="(item,index) in list" :key="index">
					<view class="" @click="checkItem(index)">

						<image :src="`${CDN}/select.png`" v-if="item.checked" class="checked-image" mode=""></image>
						<image :src="`${CDN}/not_select.png`" v-else class="checked-image" mode=""></image>
					</view>
					<view class="goodsInfo-right">
						<image :src="item.img" class="goods-image" mode=""></image>
						<view class="goodsInfo-box">
							<text class="goods-name" @click="chooseItem(item.goods_id)">{{item.name}}</text>
							<view class="goods-box">
								<text class="goods-price">¥{{item.price | doller}}</text>
								<view class="goods-num-box">
									<view class="goods-image" @click="handleItemClick('remove',item.id)">
										<text>-</text>
									</view>
									<view class="goods-num">
										<text>{{item.goods_num}}</text>
									</view>
									<view class="goods-image" @click="handleItemClick('add',item.id,item.goods_id)">
										<text>+</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="statistics-box">
				<view class="statistics">
					<view class="statistics-left" v-if="isCheckAll()" @click="handleCheckAll">
						<image :src="`${CDN}/select.png`" class="checked-image" mode="">
						</image>
						<text>全选</text>
					</view>
					<view class="statistics-left" v-else @click="handleCheckAll">
						<image :src="`${CDN}/not_select.png`" class="checked-image" mode="">
						</image>
						<text>全选</text>
					</view>
					<view class="statistics-right" v-if="isCut">
						<text>总计：</text><text class="text-color">¥{{getTotalPrice() | doller}}</text>
						<view class="btn" @click="createOrder">
							<text>结算</text>
						</view>
					</view>
					<view class="statistics-right" v-else @click="deleteGoods">
						<view class="btn">
							<text>删除</text>
						</view>
					</view>
				</view>
				<view class="gap"></view>
			</view>
		</view>
		<slot></slot>
	</view>
</template>

<script>
	import tool from "@/config/tool.js"
	export default {
		name: "cart-list",
		props: {
			list: {
				type: [Array],
				default: () => {
					return []
				}
			}
		},
		filters: {
			doller(num) {
				return num.toFixed(2)
			}
		},
		data() {
			return {
				CDN: 'https://itblog.oss-cn-hangzhou.aliyuncs.com/mall/img',
				iPhoneX: false,
				isCut: true,
				checkedAll: false,
			}
		},
		updated() {
			this.iPhoneX = uni.getStorageSync('iPhoneX')
		},
		methods: {
			createOrder() {
				const chooseGoodsList = this.list.filter(item => item.checked)
				if (chooseGoodsList.length > 0) this.$emit("onCreateOrderClick", chooseGoodsList)
				else this.$tool.message('请先选择商品')
			},
			getTotalPrice() {
				let total = 0
				this.list.forEach(e => {
					if (e.checked) {
						total += e.goods_num * e.price
					}
				})
				return total
			},
			isCheckAll() {
				return this.list.filter(e => e.checked === true).length === this.list.length
			},
			checkItem(index) {
				this.$emit("onCheckClick", index)
			},
			handleCheckAll() {
				this.$emit("onCheckAll", !this.isCheckAll())
			},
			chooseItem(id) {
				console.log(id, "chooseItem");
				this.$emit('onChooseItem', id)
			},
			deleteGoods() {
				this.$emit("onDeleteClick")
			},
			handleItemClick(addOrRemove, id, gid) {
				const param = {
					addOrRemove,
					id,
					gid
				}
				tool.throttle(() => {
					console.log(param);
					this.$emit("onNumberClick", param)
				}, 500)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.common-car {
		width: 750rpx;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		background: #F5F5F5;
	}

	.shop-car {
		width: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;


		.empty-shop-car {
			width: 750rpx;
			min-height: 680rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.empty-shop-car-image {
				width: 340rpx;
				height: 278rpx;
				margin-top: 102rpx;
			}

			text {
				margin-top: 40rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #666666;
			}

			.empty-shop-car-btn {
				width: 240rpx;
				height: 80rpx;
				background: red;
				border-radius: 200rpx;
				margin-top: 40rpx;
				text-align: center;
				line-height: 80rpx;
				font-size: 30rpx;
				font-weight: 400;
				color: #FFFFFF;
			}
		}


		.checked-image {
			width: 36rpx;
			height: 36rpx;
		}

		.shop-car-header {
			width: 690rpx;
			height: 80rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: flex-end;

			text {
				font-size: 28rpx;
				font-weight: 400;
				color: #313133;
			}
		}

		.store-box {
			width: 750rpx;
			margin-bottom: 20rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			background-color: #FFFFFF;

			.store-header {
				width: 690rpx;
				height: 78rpx;
				padding: 0 30rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				font-size: 28rpx;
				font-weight: 400;
				color: #313133;


				text {
					font-size: 28rpx;
					font-weight: 400;
					color: #313133;
					margin-left: 20rpx;
				}

				.label {
					width: 14rpx;
					height: 24rpx;
					margin-left: 10rpx;
					margin-top: 5rpx;
				}
			}

			.goodsInfo {
				width: 690rpx;
				height: 246rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				border-bottom: 2rpx solid #EDEDED;

				&:nth-last-child(1) {
					border: none;
				}


				.goodsInfo-right {
					width: 634rpx;
					height: 246rpx;
					margin-left: 20rpx;
					display: flex;
					flex-direction: row;
					align-items: center;

					.goods-image {
						width: 180rpx;
						height: 180rpx;
					}

					.goodsInfo-box {
						width: 428rpx;
						height: 180rpx;
						margin-left: 24rpx;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						align-items: center;

						.goods-name {
							// width: 428rpx;
							font-size: 26rpx;
							font-weight: 400;
							color: #313133;

							text {

								display: -webkit-box;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
							}
						}

						.spe {
							width: 428rpx;
							margin-top: 10rpx;
							font-size: 24rpx;
							font-weight: 400;
							color: #919298;
						}

						.goods-box {
							width: 428rpx;
							margin-top: 18rpx;
							display: flex;
							flex-direction: row;
							align-items: center;
							justify-content: space-between;

							.goods-price {
								font-size: 32rpx;
								font-weight: 400;
								color: red;
							}

							.goods-num-box {
								width: 168rpx;
								height: 46rpx;
								display: flex;
								flex-direction: row;
								align-items: center;

								.goods-image {
									width: 46rpx;
									height: 44rpx;
									text-align: center;
									line-height: 44rpx;
									border: 1rpx solid #CFCFCF;
									font-size: 38rpx;
								}

								.goods-num {
									width: 76rpx;
									height: 44rpx;
									text-align: center;
									line-height: 44rpx;
									font-size: 33rpx;
									font-weight: 400;
									color: #666666;
									border-top: 1px solid #CFCFCF;
									border-bottom: 1px solid #CFCFCF;
								}
							}
						}
					}
				}
			}
		}

		.statistics-box {
			width: 750rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			background-color: #FFFFFF;
			position: fixed;
			bottom: calc(100rpx + env(safe-area-inset-bottom));

			.statistics {
				// width: 720rpx;
				padding: 0 0 0 30rpx;
				height: 98rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: space-between;

				.statistics-left {
					width: 120rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: space-between;

					image {
						width: 36rpx;
						height: 36rpx;
					}

					text {
						font-size: 30rpx;
						font-weight: 400;
						color: #313133;
					}
				}

				.statistics-right {
					width: 600rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: flex-end;

					.btn {
						width: 218rpx;
						height: 98rpx;
						background: red;
						text-align: center;
						line-height: 98rpx;
						font-size: 30rpx;
						font-weight: 400;
						color: #fff;
						// margin-left: 40rpx;
						text{
							margin-right: 0;
						}
					}

					text {
						margin-right: 20rpx;
						font-size: 30rpx;
						font-weight: 400;
						color: #fff;
					}

					.text-color {
						color: rgba(242, 18, 18, 1);
					}
				}
			}

			// .gap {
			// 	width: 750rpx;
			// 	height: 40rpx;
			// }
		}
	}
</style>
